﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SecurableSource.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Collections.Generic;

namespace Smart.Common.Security
{
	/// <summary>
	/// Source for securable objects
	/// </summary>
	public abstract class SecurableSource
	{
		/// <summary>
		/// Security context
		/// </summary>
		public SecurityContext Ctx { get; set; }

		/// <summary>
		/// Create new securable filter
		/// </summary>
		public abstract SecurableFilter CreateFilter();

		/// <summary>
		/// Get securable types
		/// </summary>
		public abstract ICollection<SecurableType> GetSecurableTypes();

		/// <summary>
		/// Detect securable type
		/// </summary>
		public abstract SecurableType DetectSecurableType(string instanceId);

		/// <summary>
		/// Search for specified securable object
		/// </summary>
		public abstract ISecurable LoadSecurable(string instanceId);

		/// <summary>
		/// Search for all securable objects of specified type
		/// </summary>
		public abstract ICollection<ISecurable> SearchSecurables(SecurableType securableType);

		/// <summary>
		/// Search for filtered securable objects of specified type
		/// </summary>
		public abstract ICollection<ISecurable> SearchSecurables(SecurableType securableType, SecurableFilter filter);

		/// <summary>
		/// Detects security policy for securable object
		/// </summary>
		public abstract SecurityPolicy DetectSecurityPolicy(SecurableType securableType, ISecurable securable);

		/// <summary>
		/// Load values into context
		/// </summary>
		public virtual void LoadValuesToCtx(IDictionary<string, object> values)
		{
			if (values == null)
				return;

			var ctxValues = Ctx.Values;

			foreach (var kvp in values)
				ctxValues[kvp.Key] = kvp.Value;
		}

		/// <summary>
		/// Source for securable objects
		/// </summary>
		protected SecurableSource(SecurityContext ctx)
		{
			Ctx = ctx;
		}
	}
}